﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using ImageConverter;
using Shared;
 

namespace OnlineShop
{
    public partial class shopForm : Form, IMainView
    {

        public event Action CategoryChanged;
        public event Action RowSelected;
        public event Action Switch_clicked;
        
        public shopForm()
        {
            InitializeComponent();
            button1.Click+= (sender, args) => Invoke(Switch_clicked);

            categoryCmb.SelectedIndexChanged += (sender, args) => Invoke(CategoryChanged);
            dataGridView1.DoubleClick += (sender, args) =>
            {
                if (((DataGridView)sender).SelectedRows != null)
                {
                    Invoke(RowSelected);
                }
            };
        }

        public void SetCurrentUser(string user)
        {
            loginTB.Text = user;
        }
        public void SetPictureSet(Image im)
        {
            pictureBox.Image = im;
        }

        public void SetId(string id)
        {
            IdTb.Text = id;
        }

        public void SetName(string name)
        {
            NameTb.Text = name;
        }

        public void SetPrice(string price)
        {
            PriceTB.Text = price;
        }

        public void SetDescription(string descr)
        {
            descrTB.Text = descr;
        }

        public int IdFromDataGrid()
        {
            return (int)dataGridView1.CurrentRow.Cells[0].Value;
        }

        public void SetListCategories(ICollection<string> categories)
        {
            categoryCmb.DataSource = categories;
            
        }

        public ICollection<Product> tableProducts
        {
            set
            {               
                dataGridView1.DataSource = value;
               
            }
        }

        public string ProductCategoryFromProdInfo
        {
            get
            {
                return categoryCmb.Text;
            }
            set
            {
                categoryCmb.Text = value;
            }
        }

        public string ReturnSelectedCategoryTable()
        {
            return categoryCmb.Text;
        }

        public void SetListCategoriesForProduct(ICollection<string> categories)
        {
            categoryCmb.DataSource = categories;           
        }

        public ICollection<string> ProductCategoriesFromTable
        {
            get
            {
                return (ICollection<string>)categoryCmb.DataSource;
            }
        }

        public int ReturnSelectedProductId()
        {
            return Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
        }

        private void Invoke(Action action)
        {
            if (action != null) action();
        }

        public new void Show()
        {
            Application.Run(this);
        }       
    }
}
